/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Taikhoan;
import pojo.Thongtintaikhoan;
import util.HibernateUtil;

/**
 *
 * @author WMThiet
 */
public class ThongTinTaiKhoanDAO {
    public static String LayEmailNguoiDung(String userRemind) {
        
        String email = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            
            String hql = "from Thongtintaikhoan tk where tk.taikhoan.tenTaiKhoan=:tenTK";
            Query query = session.createQuery(hql);
            query.setString("tenTK", userRemind);
            
            List<Thongtintaikhoan> listTTTK = query.list();                        
            email = listTTTK.get(0).getEmail();
            
        } catch (Exception ex) {
            email = null;
        } finally {
            session.close();
        }
        
        return email;
    }
    
    public static Thongtintaikhoan LayThongTinNguoiDung(int maTK) {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Thongtintaikhoan thongtinTK = null;
        try {
            session.beginTransaction();
            
            String hql = "from Thongtintaikhoan tk where tk.taikhoan.maTaiKhoan=:matk";
            Query query = session.createQuery(hql);
            query.setInteger("matk", maTK);
            
            thongtinTK = (Thongtintaikhoan)query.uniqueResult();            
            
        } catch (Exception ex) {
            thongtinTK = null;
        } finally {
            session.close();
        }
        
        return thongtinTK;
    }
    
        public static Thongtintaikhoan layThongTinTaiKhoan(Taikhoan tk)
    {
        boolean bDone = false;
        Thongtintaikhoan tttk = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            String hql = "from Thongtintaikhoan tk where tk.taikhoan.tenTaiKhoan=:tenTK";
            Query query = session.createQuery(hql);
            query.setString("tenTK", tk.getTenTaiKhoan());
            List<Thongtintaikhoan> listTTTK = query.list();                        
            tttk = listTTTK.get(0);
        } catch (Exception ex) {
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
        return tttk;
    }
    
    public static boolean capNhatThongTin(Thongtintaikhoan tttk)
    {
        boolean kq = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(tttk);
            transaction.commit();
            kq = true;
        } catch (Exception ex) {
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return kq;
    }
}
